﻿using System;
using Jxym.Common.JSON;
using Newtonsoft.Json;
using Jxym.DBUtility;
using Jxym.wlkc2011.Module.Default.model;
using System.Data.SqlClient;
using System.Collections.Generic;
using Jxym.wlkc2011.Module.CMS.MODEL;
using Jxym.Common.MyFile;
using System.Reflection;
using System.Collections;
using System.IO;

namespace Jxym.wlkc2011.Module.CMS.SERVER
{
    public partial class siteAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MsSqlDal<Cms_Site> iUtm = new MsSqlDal<Cms_Site>();
                UpLoad upload = new UpLoad();
                string siteName = "";
                if (Request.Params["siteid"] != null)
                {
                    siteName = Request.Params["siteid"];
                    string path = Server.MapPath("~/UpFiles/" + siteName);
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    upload.RootPath = "~/UpFiles/" + siteName;
                    upload.AllowExts = "swf|jpg|png|gif";
                    upload.IsExtClass = false;
                    upload.Save();
                    string[] filenames = upload.FileNames.Split('|');

                    PropertyInfo[] fields = typeof(Cms_Site).GetProperties();
                    List<string> listName = new List<string>();
                    List<string> listValue = new List<string>();
                    object identity = 0;
                    foreach (PropertyInfo property in fields)
                    {
                        if (Request.Params[property.Name] != null)
                        {
                            listName.Add("@" + property.Name);
                            listValue.Add(Request.Params[property.Name]);
                        }
                    }
                    if (Request.Params["IsDownModel"] == null)
                    {
                        listName.Add("@IsDownModel");
                        listValue.Add("no");
                    }
                    if (filenames[0] != "")
                    {
                        listName.Add("@Banner");
                        listValue.Add(filenames[0]);
                    }
                    if (Request.Params["ID"] == null || Request.Params["ID"] == "0")
                    {
                        listName.Add("@UserID");
                        listValue.Add(Jxym.Common.Request.AdminID("user"));
                    }

                    SqlParameter[] parameters = new SqlParameter[listName.Count];

                    for (int i = 0; i < listName.Count; i++)
                    {
                        parameters[i] = new SqlParameter(listName[i], listValue[i]);
                    }
                    if (listName.Count > 0)
                    {
                        if (Request.Params["id"] != null && Request.Params["id"] != "0")
                        {
                            identity = iUtm.Updata(parameters);
                            Response.Write("{'success':true,msg:'信息更新完成'}");
                        }
                        else
                        {
                            identity = iUtm.Add(parameters);

                            int identityId = int.Parse(identity.ToString());

                            ///*额外需求 start*///
                            if (identityId > 0)
                            {
                                object[] parentClass = new string[] { "课程描述", "主讲人", "课程学习", "相关资源" };

                                MsSqlDal<Cms_ContentClass> mccc = new MsSqlDal<Cms_ContentClass>();

                                foreach (string name in parentClass)
                                {
                                    SqlParameter[] paeames = new SqlParameter[] {
                                        new SqlParameter("@Title",name),
                                        new SqlParameter("@TypeID","0"),
                                        new SqlParameter("@URL","#"),
                                        new SqlParameter("@siteName",siteName),
                                        new SqlParameter("@ParentID","0")
                                    };

                                    object classid = mccc.Add(paeames);
                                }
                            }
                            ///*额外需求 end*///

                            Response.Write("{'success':true,msg:'信息添加完成'}");
                        }
                    }
                    else
                    {
                        Response.Write("{'success':false,'msg':'没有可提交的数据,本次操作终止'}");
                    }
                }
                else
                {
                    Response.Write("{'success':false,msg:'无法获取专题名称'}");
                    return;
                }
            }
        }
    }
}